/**
 * made by xufeng
 * 2009-5-22 下午02:40:11
 *  
 */
package com.litxuf.web.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.litxuf.FinalInformation;
import com.litxuf.logic.admin.dao.model.UserAdmin;
import com.litxuf.logic.admin.dao.model.UserPower;
import com.litxuf.logic.admin.manager.UserAdminManager;
import com.litxuf.logic.core.ManagerFactory;
import com.yeqiangwei.util.ParamUtils;

/**
 * 
 *
 */
public class UserAdminAndUserPowerAction extends DispatchAction {
	Logger logger = Logger.getLogger(this.getClass().getName());
	UserAdminManager am = ManagerFactory.getFactory().getAdminManager();
	public ActionForward pwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String pwd1 = request.getParameter("pwdchange");
		String pwd2 = request.getParameter("pwdchange2");
		if (!pwd1.equals(pwd2)) {
			request.setAttribute("Result", "密码不能为空");
			return mapping.findForward("globalResult");
		}

		UserAdmin user = (UserAdmin) request.getSession().getAttribute(FinalInformation.UserAdiminOperator);
		if (user == null) {
			request.setAttribute("RESULT", "未登录 出现异常,稍候再试啊..");
		} else {
			try{
				am.updatePwd(user.getUserId(),pwd1);
				request.setAttribute("RESULT", "修改密码成功");
			}catch(Exception e){
				request.setAttribute("RESULT", "出现异常,稍候再试...");
				
			}
		 

		}
		return mapping.findForward("globalResult");
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");

		logger.info(":: 用户(=" + username + ")  ");
		
		UserAdmin item=am.findUserAdminByUserName(username);
		
		if(item!=null){
			request.setAttribute("ERROR", "用户名重复了");
			request.setAttribute("username",username);
			return mapping.findForward("create");
		}
		UserAdmin us=new UserAdmin();
		
		us.setAvailable("Y");
		us.setUsername(username);
		us.setPassword("000000");
		us.setUserlevel("1");
		
		String id=am.save(us);
		logger.info(":: 用户(=" + username + ") id=("+id+") ");
 
		request.setAttribute("RESULT", "新建管理员成功 初始密码 000000 你现在可以去 '管理员维护' 维护其权限了" );
		return mapping.findForward("result");

	}
	

 

	public ActionForward initColumn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String userId = request.getParameter("userId");

		logger.info("::准备修改用户(id=" + userId + ")的栏目权限 ");

		List ls = am.findUserPower2AdminColum(userId);
		request.setAttribute("COLUMN", ls);
		request.setAttribute("userId", userId);
		return mapping.findForward("column");

	}

	public ActionForward ls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		List ls = am.findAllBasicUser();
		request.setAttribute("ALLUSER", ls);
		return mapping.findForward("ls");

	}

	public ActionForward savepower(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String powerType = request.getParameter("powerType");
		String userId = request.getParameter("userId");

		String powerCodes[] = request.getParameterValues("powerCodes");

		logger.info(" 修改 userId(" + userId + ") powerType(" + powerType
				+ ") 的权限数据");
		 
		List ls = new ArrayList();

		for (String powerCode : powerCodes) {
			UserPower up = new UserPower();
			up.setPowerCode(powerCode);
			up.setPowerType(powerType);
			up.setUserId(userId);
			ls.add(up);

			logger.info("--> PowerCode =" + powerCode);
		}

		am.saveAreaPowerList(ls, userId, powerType);
		request.setAttribute("RESULT", "操作成功");
		return mapping.findForward("result");

	}

	public ActionForward avail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		int userId = ParamUtils.getIntParameter(request, "userId");
		String flag = ParamUtils.getStringParameter(request, "flag", "Y");// 默认启用

		UserAdmin inf = am.findByUserId(userId);
		if (inf != null) {
			inf.setAvailable(flag);
			try {
				am.updateUser(inf);
			} catch (Exception e) {
				logger.info(ExceptionUtils.getFullStackTrace(e));
				e.printStackTrace();
				request.setAttribute("RESULT", "操作用户是否可用 出现异常 请联系管理员");
			}

		} else {
			request.setAttribute("RESULT", "操作用户是否可用 出现异常 请联系管理员");
		}

		if (flag.equals("Y")) {
			request.setAttribute("RESULT", "操作成功  用户(" + inf.getUsername()
					+ ")已启用 可以登录了 ");

		} else {
			request.setAttribute("RESULT", "操作成功 用户(" + inf.getUsername()
					+ ") 已禁止登录");
		}
		return mapping.findForward("result");

	}

	 
	 
}
